/**
 * PLM项目Store统一配置入口
 */

// 引入MobX配置工具并立即配置MobX
const mobxConfig = require('./helpers/mobx_config.js');

// 配置MobX为宽松模式，避免状态修改错误
mobxConfig.configureMobX();

try {
  // 导入plmStore
  const plmStore = require('./plm_store.js');
  
  // 导入projectStore并检查是否包含关键方法
  const { projectStore } = require('./project_store.js');
  console.log('导入projectStore:', {
    hasStore: !!projectStore,
    hasClearProject: typeof projectStore.clearProject === 'function',
    hasLoadProjectList: typeof projectStore.loadProjectList === 'function'
  });
  
  // 导入其他store
  const researchStore = require('./research_store.js');
  const researchStateStore = require('./research_state_store.js');
  const roleSimulatorStore = require('./role_simulator_store.js');
  const favFootStore = require('./fav_foot_store.js');
  
  // 使用安全包装的plmProjectStore
  // 这会替代全局的plmProjectStore，提供更安全的状态管理
  const plmProjectStore = require('./plm_project_store_wrapper.js');
  
  // 将全局plmProjectStore作为备用，如果需要的话
  let globalPlmProjectStore;
  try {
    const globalStoreConfig = require('../config/path_resolver.js').getRootConfigPath('path_resolver.js').getProjectStorePath('modules/project/store_config.js');
    globalPlmProjectStore = globalStoreConfig.plmProjectStore;
    console.log('成功加载全局plmProjectStore(备用)');
  } catch (error) {
    console.error('加载全局plmProjectStore失败，使用空对象替代', error);
    // 提供空实现，避免错误
    globalPlmProjectStore = {
      loading: false,
      projects: [],
      currentProject: null,
      loadProjects: () => Promise.resolve([])
    };
  }
  
  module.exports = {
    plmStore,
    projectStore,
    researchStore,
    researchStateStore,
    roleSimulatorStore,
    favFootStore,
    
    // 导出安全包装的plmProjectStore
    plmProjectStore,
    
    // 导出原始全局plmProjectStore作为备用
    globalPlmProjectStore,
    
    // 导出MobX配置工具，便于在其他地方使用
    mobxConfig
  };
} catch (e) {
  console.error('store/index.js加载模块失败:', e);
  
  // 出错时提供最小实现，确保不会导致引用错误
  module.exports = {
    plmStore: { clearCache: () => console.warn('空plmStore.clearCache') },
    projectStore: { 
      projectList: [],
      currentProject: null,
      clearProject: () => console.warn('空projectStore.clearProject'),
      loadProjectList: () => Promise.resolve([]),
      loadProjectDetail: () => Promise.resolve(null)
    },
    researchStore: {},
    researchStateStore: {},
    roleSimulatorStore: {},
    favFootStore: {},
    plmProjectStore: { clearAll: () => console.warn('空plmProjectStore.clearAll') },
    globalPlmProjectStore: {},
    mobxConfig: { configureMobX: () => {} }
  };
} 